package work2;

import java.util.Scanner;

public class studentsystem {

    static Student[] stus=new Student[0];
    public static void menu() {
        System.out.println("欢迎使用学生信息录入系统!");
        System.out.println("""
                1.添加学生信息
                2.修改学生信息
                3.删除学生信息
                4.查询所有的学生信息
                5.根据学号查询学生信息
                6.根据姓名查询学生信息
                7.根据学生成绩进行从大到小的排序展示
                8.退出系统
                请选择功能：""");
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            menu();
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    // 添加学生信息
                    int id;
                    System.out.print("请输入学号：");
                    while(true) {
                        try{
                            id = scanner.nextInt();
                            // 检查学号，当学号输入范围不正确或者不是数字时，提示用户重新输入
                            if (id < 1000 || id > 9999 ) {
                                System.out.println("学号输入不正确，请重新输入！");
                                continue;
                            }
                            // 检查学号是否已存在
                            boolean exists = false;
                            for (Student stu : stus) {
                                if (stu != null && stu.getNo() == id) {
                                    exists = true;
                                    break;
                                }
                            }
                            if (exists) {
                                System.out.println("学号已存在，请重新输入！");
                            } else {
                                break;
                            }
                        }catch (Exception e) {
                            System.out.println("学号必须为整数，请重新输入！");
                            scanner.next(); // 清除错误输入
                        }
                    }

                    System.out.print("请输入姓名：");
                    String name = scanner.next();

                    System.out.print("请输入成绩：");
                    double score;
                    while (true) {
                        try{
                            score = scanner.nextDouble();
                            // 检查成绩范围
                            if (score < 0 || score > 100) {
                                System.out.println("成绩输入不正确，请重新输入！");
                                continue;
                            }
                            break;
                        }catch (Exception e) {
                            System.out.println("成绩必须为数字，请重新输入！");
                            scanner.next(); // 清除错误输入
                        }
                    }
                    // 存储学生信息
                    Student newStudent = new Student(id, name, score);
                    // 扩展数组
                    Student[] newStus = new Student[stus.length + 1];
                    for (int i = 0; i < stus.length; i++) {
                        newStus[i] = stus[i];
                    }
                    newStus[stus.length] = newStudent;
                    stus = newStus;
                    System.out.println("学生信息添加成功！");
                    break;
                case 2:
                    while (true) {
                        System.out.println("请输入要修改的学生学号：");
                        try {
                            int modifyId = scanner.nextInt();
                            boolean b = false;
                            for (int i = 0; i < stus.length; i++) {
                                if (stus[i] != null && stus[i].getNo() == modifyId) {
                                    b = true;
                                    System.out.print("请输入新的姓名：");
                                    String newName = scanner.next();
                                    stus[i].setName(newName);

                                    System.out.print("请输入新的成绩：");
                                    while (true) {
                                        double newScore = scanner.nextDouble();
                                        // 检查成绩范围
                                        if (newScore < 0 || newScore > 100) {
                                            System.out.println("成绩格式不正确，请重新输入！");
                                            continue;
                                        }
                                        stus[i].setScore(newScore);
                                        break;
                                    }
                                    System.out.println("学生信息修改成功！");
                                    break;
                                }
                            }
                            if (b) {
                                break;
                            } else {
                                System.out.println("学号不存在，请重新输入！");
                            }
                        } catch (Exception e) {
                            System.out.println("学号必须为整数，请重新输入！");
                            scanner.next(); // 清除错误输入
                        }
                    }
                    break;
                case 3:
                    System.out.println("请输入要删除的学生学号：");
                    while (true) {
                        try {
                            boolean b = false;
                            int index=-1;
                            int deleteId = scanner.nextInt();
                            for (int i = 0; i < stus.length; i++) {
                                if (stus[i].getNo() == deleteId) {
                                    b = true;
                                    index = i;
                                    stus[i] = stus[stus.length - 1];
                                    break;
                                }
                            }
                            if (b) {
                                //创建一个新的数组，长度为原数组减一
                                Student[] newStudents = new Student[stus.length - 1];
                                for(int i = 0;i<newStudents.length; i++) {
                                    if (i < index) {
                                        newStudents[i] = stus[i];
                                    } else {
                                        newStudents[i] = stus[i + 1];
                                    }
                                }
                                //将新数组赋值给原数组
                                stus = newStudents;
                                System.out.println("学生信息删除成功！");
                                break;
                            } else {
                                System.out.println("学号不存在，请重新输入！");
                            }
                        } catch (Exception e) {
                            System.out.println("学号必须为整数，请重新输入！");
                            scanner.next(); // 清除错误输入
                        }
                    }
                    break;
                case 4:
                    int count =0;
                    for(int i =0; i < stus.length; i++) {
                        if (stus[i] != null) {
                            count++;
                        }
                    }
                    if (count == 0) {
                        System.out.println("没有学生信息，请添加！");
                    }else {
                        System.out.println("所有学生信息如下：");
                        for (int i = 0; i <stus.length; i++) {
                            if (stus[i].getNo() != 0) {
                                System.out.println("学号：" + stus[i].getNo() + ", 姓名：" + stus[i].getName() + ", 成绩：" + stus[i].getScore());
                            }
                        }
                    }
                    break;
                case 5:
                    System.out.println("请输入要查询的学生学号：");
                    while (true) {
                        try {
                            int queryId = scanner.nextInt();
                            boolean b = false;
                            for (int i = 0; i < stus.length; i++) {
                                if (stus[i].getNo() == queryId) {
                                    b = true;
                                    System.out.println("学号：" + stus[i].getNo() + ", 姓名：" + stus[i].getName() + ", 成绩：" + stus[i].getScore());
                                    break;
                                }
                            }
                            if (b) {
                                break;
                            } else {
                                System.out.println("学号不存在，请重新输入！");
                            }
                        } catch (Exception e) {
                            System.out.println("学号必须为整数，请重新输入！");
                            scanner.next(); // 清除错误输入
                        }
                    }
                    break;
                case 6:
                    System.out.println("请输入要查询的学生姓名：");
                    while (true) {
                        String queryName = scanner.next();
                        boolean b = false;
                        for (int i = 0; i < stus.length; i++) {
                            if (stus[i].getName() != null && stus[i].getName().equals(queryName)) {
                                b = true;
                                System.out.println("学号：" + stus[i].getNo() + ", 姓名：" + stus[i].getName() + ", 成绩：" + stus[i].getScore());
                            }
                        }
                        if(b){
                            break;
                        } else {
                            System.out.println("姓名不存在，请重新输入！");
                        }
                    }
                    break;
                case 7:
                    // 检查是否有学生信息
                    if (stus.length == 0) {
                        System.out.println("没有学生信息，请添加！");
                        break;
                    }
                    System.out.println("根据学生成绩进行从大到小的排序展示");
                    // 创建一个索引数组
                    int[] indices = new int[stus.length];
                    for (int i = 0; i < stus.length; i++) {
                        indices[i] = i;
                    }
                    // 使用冒泡排序对学生成绩进行排序
                    for (int i = 0; i < stus.length - 1; i++) {
                        for (int j = i+1; j < stus.length; j++) {
                            if (stus[indices[i]] != null && stus[indices[j]] != null &&stus[indices[i]].getScore() < stus[indices[j]].getScore()) {
                                // 交换索引
                                int temp = indices[i];
                                indices[i] = indices[j];
                                indices[j] = temp;
                            }
                        }
                    }
                    // 输出排序后的学生信息
                    for (int i = 0; i < stus.length; i++) {
                        if (stus[indices[i]] != null) {
                            System.out.println("学号：" + stus[indices[i]].getNo() + ", 姓名：" + stus[indices[i]].getName() + ", 成绩：" + stus[indices[i]].getScore());
                        }
                    }
                    break;
                case 8:
                    System.out.println("已成功退出系统！");
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效的选择！");
            }
        }
    }
}

